package com.example.meeting.service.impl;

import com.example.meeting.mapper.MeetingMapper;
import com.example.meeting.mapper.PersonMapper;
import com.example.meeting.mapper.SubjectMapper;
import com.example.meeting.mapper.ToDoMapper;
import com.example.meeting.model.domain.Meeting;
import com.example.meeting.model.domain.Person;
import com.example.meeting.model.domain.Subject;
import com.example.meeting.service.ISubjectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class SubjectServiceImpl implements ISubjectService {
    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private MeetingMapper meetingMapper;

    @Autowired
    private ToDoMapper todoMapper;

    @Override
    public Subject selectSubjectById(Long id) {
        try {
            return subjectMapper.selectSubjectById(id);
        } catch (Exception e) {
            log.error("查询议题接口报错！{}", e.getMessage());
        }
        return null;
    }

    @Override
    public PageInfo<Subject> selectSubjectList(Integer page, Integer pageSize, Subject subject) {
        try {
            PageHelper.startPage(page, pageSize);
            List<Subject> subjectList = subjectMapper.selectSubjectList(subject);
            return new PageInfo<>(subjectList);
        } catch (Exception e) {
            log.error("查询议题列表接口报错！{}", e.getMessage());
        }
        return null;
    }

    private static final Object insertLock = new Object();
    @Transactional
    @Override
    public Integer addSubject(Subject subject) {
        try {
            synchronized (insertLock) {
                subjectMapper.addSubject(subject);
                if (subject.getPersonList()!=null && subject.getPersonList().size()>0){
                    subjectMapper.addSubjectPerson(subject.getId(),subject.getPersonList());

                    //获取会议
                    Meeting meet =meetingMapper.selectMeetingById(subject.getMeeting().getId());
                    //获取会议的截至时间
                    Date endTime = meet.getEndTime();
                    //整合待做信息 ${会议名称} 会议的${议题名称} 议题已发布，请及时处理
                    String todo = meet.getName()+" 会议的"+subject.getName()+" 议题已发布，请及时处理";
                    //新增待做信息
                    for(Person person:subject.getPersonList()) {
                        todoMapper.addTodo(person.getId().intValue(),todo,0,endTime);
                    }
                }
            }
        } catch (Exception e) {
            log.error("新增议题接口报错！{}", e.getMessage());
        }
        return 0;
    }

    private static final Object updateLock = new Object();
    @Transactional
    @Override
    public Integer updateSubject(Subject subject) {
        try {
            synchronized (updateLock) {
                subjectMapper.updateSubject(subject);
                subjectMapper.deleteSubjectPerson(subject.getId());
                if (subject.getPersonList() != null && subject.getPersonList().size() > 0) {
                    subjectMapper.addSubjectPerson(subject.getId(), subject.getPersonList());
                }
            }
        } catch (Exception e) {
            log.error("更新议题接口报错！{}", e.getMessage());
        }
        return 0;
    }

    @Override
    public Integer updateSubjectStatus(Subject subject) {
        return subjectMapper.updateSubjectStatus(subject);
    }

    @Override
    @Transactional
    public Integer updateSubjectWeightBatch(List<Subject> subjectList) {
        try {
            return subjectMapper.updateSubjectWeightBatch(subjectList);
        } catch (Exception e) {
            log.error("删除议题接口报错！{}", e.getMessage());
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return 0;
    }

    @Override
    public Integer deleteSubject(Long id) {
        try {
            return subjectMapper.deleteSubject(id);
        } catch (Exception e) {
            log.error("删除议题接口报错！{}", e.getMessage());
        }
        return 0;
    }

    @Override
    public Integer deletePersonAndSub(Integer s_id, Integer p_id) {
        try {
            return subjectMapper.deletePersonAndSub(s_id,p_id);
        } catch (Exception e) {
            log.error("删除议题成员中间表接口报错！{}", e.getMessage());
        }
        return 0;
    }

    @Override
    public Integer addSubjectPerson(Long subjectId, List<Person> personList) {
        try{
            return subjectMapper.addSubjectPerson(subjectId,personList);
        }catch (Exception e){
            log.error("添加议题成员信息接口报错!{}",e.getMessage());
        }
        return 0;
    }


}
